package com.wayne.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.wayne.model.User;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
  @Override
  public void insertFill(MetaObject metaObject) {
    Class<?> modelClazz = metaObject.getOriginalObject().getClass();
    if (modelClazz.equals(User.class)) {
      Object email = getFieldValByName("email", metaObject);
      if (null == email) {
        // 字段为空，可以进行填充
        setFieldValByName("email", String.format("%s@163.com", getFieldValByName("name", metaObject).toString()), metaObject);
      }
    }
  }

  @Override
  public void updateFill(MetaObject metaObject) {

  }
}
